<template>
  <div>
    <el-table :data="tableData"
              border
              style="width: 100%">
      <el-table-column prop="role"
                       label="角色"
                       width="180">
      </el-table-column>
      <el-table-column prop="description"
                       label="描述">
      </el-table-column>
      <el-table-column label="操作"
                       width="350">
        <template slot-scope="scope">
          <el-button size="mini"
                     @click="updatePrem(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini"
                     type="danger"
                     @click="deletPrem(scope.$index, scope.row)">删除</el-button>
          <el-popover placement="left"
                      width="400"
                      trigger="click">
            <el-table :data="permData">
              <el-table-column width="150"
                               prop="perm"
                               label="权限名"></el-table-column>
              <el-table-column width="100"
                               prop="description"
                               label="权限描述"></el-table-column>
              <el-table-column width="100"
                               label="操作">
                <template slot-scope="scope1">
                  <el-button size="mini"
                             @click="onRowClick(scope1.row)">添加</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-button slot="reference"
                       @click="set(scope.$index,scope.row)">设置权限</el-button>
          </el-popover>
          <el-popover placement="left"
                      width="400"
                      trigger="click">
            <el-table :data="permData">
              <el-table-column width="150"
                               prop="perm"
                               label="权限名"></el-table-column>
              <el-table-column width="100"
                               prop="description"
                               label="权限描述"></el-table-column>
            </el-table>
            <el-button slot="reference"
                       @click="get(scope.$index,scope.row)">查看权限</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title"
               :visible.sync="editFormVisible"
               width="30%"
               @click="closeDialog">
      <el-form label-width="120px"
               :model="editForm"
               ref="editForm">
        <el-form-item label="角色"
                      prop="role">
          <el-input size="small"
                    v-model="editForm.role"
                    auto-complete="off"
                    placeholder="请输入权限"></el-input>
        </el-form-item>
        <el-form-item label="描述"
                      prop="description">
          <el-input size="small"
                    v-model="editForm.description"
                    auto-complete="off"
                    placeholder="请描述权限"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button size="small"
                   @click="closeDialog">取消</el-button>
        <el-button size="small"
                   type="primary"
                   class="title"
                   @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
    <el-button size="small"
               type="primary"
               icon="el-icon-plus"
               @click="updatePrem()">添加</el-button>
  </div>
</template>

<script>
export default {
  data () {
    return {
      tableData: [],
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      roleId: "",
      editForm: {
        role: "",
        description: "",
        flag: "",
        id: ""
      },
      permData: [
      ]
    }
  },
  created () {
    this.getdata()
  },
  inject: ["refresh"],
  methods: {
    getdata () {
      this.$axios.get('/api/role/findAll').then(res => {
        if (res.data.code == 200) {
          console.log(res);
          this.tableData = res.data.data
        }
      })
    },
    updatePrem: function (index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.role = row.role
        this.editForm.description = row.description
        this.editForm.id = row.id
        this.editForm.flag = "revise"
      } else {
        this.title = '添加'
        this.editForm.role = ''
        this.editForm.description = ''
        this.editForm.flag = 'add'
      }
    },
    set (index, row) {
      console.log(row.id);
      this.$axios.get("/api/perm/findPerm/" + row.id).then(res => {
        if (res.data.code == 200) {
          this.roleId = row.id;
          this.permData = res.data.data
        }
      })
    },
    get (index, row) {
      console.log(row);
      this.$axios.get("/api/perm/findPerm1/" + row.id).then(res => {
        if (res.data.code == 200) {
          this.roleId = row.id;
          this.permData = res.data.data
        }
      })
    },
    submitForm (editData) {
      this.$refs[editData].validate(valid => {
        if (this.editForm.flag == "add") {
          this.$axios.post("/api/role/add", this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.data.code == 200) {
                this.refresh()
              } else {

              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('系统维护......请稍后再试！')
            })
        }
        if (this.editForm.flag == "revise") {
          console.log(this.editForm);
          this.$axios.post("/api/role/update", this.editForm)
            .then(res => {
              this.editFormVisible = false
              if (res.data.code == 200) {
                this.refresh()
              } else {
                alert("失败")
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('系统维护......请稍后再试！')
            })
        }

      })
    },
    deletPrem (index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios.get("/api/role/delet/" + row.id)
            .then(res => {
              if (res.data.code == 200) {
                this.refresh()
              } else {
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('系统维护中')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    closeDialog () {
      this.editFormVisible = false
    },
    onRowClick (row) {
      this.$axios.post("/api/role/addPerm/" + row.id + "/" + this.roleId)
        .then(res => {
          if (res.data.code == 200) {
            alert("添加成功")
          } else {
            console.log(res);
            alert(res.data.msg)
          }
        })
    }
  }
}
</script>